/**
 * 
 */
package com.cw.iwc.cooldown;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import android.util.Log;

/**
 * @author Steven
 *
 */
public class BaseCDTimer implements ICDTimer, CDConstants {
    
    private long mMinTime;
    private long mMaxTime;
    
    private long mTempTime;
    private long mElapseTime;
    
    private Timer mCDTimer;
    private Timer mUpdateTimer;
    
    private int mCDState;
    
    private int mUpdateInterval;
    private ICDUpdateListener mUpdateListener;
    
    /**
     * @param updateListener CD状态变化监听器  
     * @param updateInterval 更新的间隔时间(ms)
     */
    public BaseCDTimer(ICDUpdateListener updateListener, int updateInterval) {
        this.mUpdateListener = updateListener;
        this.mUpdateInterval = updateInterval;
        
        this.mElapseTime = 0;
        this.mMinTime = 0;
        this.mMaxTime = DEFAULT_MAX_CD_TIME;
    }
    
    /*
     * @see com.cw.iwc.cooldown.ICDTimer#initTimer()
     */
    public void initTimer(int minTime, int maxTime) {
        mMinTime = minTime;
        mMaxTime = maxTime;        
    }
    
    /* 
     * @see com.cw.iwc.cooldown.ICDTimer#startTimer()
     */
    public void startTimer() {
        if(mCDState == CD_STATE_RUNNING) {
            return;
        }
        
        // 每次startTimer都要重新new一个Timer出来，因为之前可能调过cancel，导致Timer不可用
        mCDTimer = new Timer();
        mUpdateTimer = new Timer();
        
        mCDState = CD_STATE_RUNNING;
        
        if(mUpdateListener == null) {
            Log.e(TAG, "mUpdateListener is null");
            return;
        }
        mUpdateListener.onCDStart();
        
        mTempTime = System.currentTimeMillis();
        // 每次schedule都要重新new一个TimerTask出来，否则会报已经调度的异常
        mCDTimer.schedule(new CDTimerTask(), 
                new Date(System.currentTimeMillis() + (mMaxTime - mMinTime - mElapseTime)));
        mUpdateTimer.scheduleAtFixedRate(new UpdateTimerTask(), 
                new Date(System.currentTimeMillis()), mUpdateInterval);
    }

    /* 
     * @see com.cw.iwc.cooldown.ICDTimer#resetTimer()
     */
    public void resetTimer() {
        if(mCDState == CD_STATE_IDLE) {
            return;
        }
        
        mCDState = CD_STATE_IDLE;
        
        if(mUpdateListener == null) {
            Log.e(TAG, "mUpdateListener is null");
            return;
        }
        mUpdateListener.onCDReset();
        
        mCDTimer.cancel();
        mUpdateTimer.cancel();
        
        mElapseTime = 0;
    }

    /* 
     * @see com.cw.iwc.cooldown.ICDTimer#setTimer()
     */
    public void setTimer(int currentTime) {
        mElapseTime = currentTime;
        
        if(mCDState == CD_STATE_RUNNING) {
            // 重新设置计时器
            mCDTimer.cancel();
            mCDTimer = new Timer();
            mCDTimer.schedule(new CDTimerTask(), 
                    new Date(System.currentTimeMillis() + (mMaxTime - currentTime)));
        }
    }
    
    class CDTimerTask extends TimerTask {
        @Override
        public void run() {
            mUpdateTimer.cancel();
            
            if(mUpdateListener == null) {
                Log.e(TAG, "mUpdateListener is null");
                return;
            }
            mUpdateListener.onCDFinished();            
        }        
    }
    
    class UpdateTimerTask extends TimerTask {
        @Override
        public void run() {
            mElapseTime = System.currentTimeMillis() - mTempTime;
            
            if(mUpdateListener == null) {
                Log.e(TAG, "mUpdateListener is null");
                return;
            }
            mUpdateListener.onCDUpdate(mElapseTime);            
        }        
    }

}
